package database;

import classes.Type;

public class DBType {
	
	private static DBType instance;
	private Type[] types;
	
	private DBType(){
		types = new Type[17];

		types[0] = new Type("Bug", null, null);
		types[1] = new Type("Dark", null, null);
		types[2] = new Type("Dragon", null, null);
		types[3] = new Type("Electric", null, null);
		types[4] = new Type("Fight", null, null);
		types[5] = new Type("Fire", null, null);
		types[6] = new Type("Flying", null, null);
		types[7] = new Type("Ghost", null, null);
		types[8] = new Type("Ground", null, null);		
		types[9] = new Type("Ice", null, null);
		types[10] = new Type("Normal", null, null);
		types[11] = new Type("Plant", null, null);
		types[12] = new Type("Poison", null, null);
		types[13] = new Type("Psychic", null, null);
		types[14] = new Type("Rock", null, null);
		types[15] = new Type("Steel", null, null);
		types[16] = new Type("Water", null, null);
	}
	
	public Type getType(String type){
		if (this.types.length <= 0) return null;

		int start = 0;
		int end = this.types.length - 1;
		int middle = (end + start) / 2;
		
		int comparison = 0;
		while (start <= end){
			comparison = type.compareToIgnoreCase(this.types[middle].getName());
			
			if (comparison == 0){
				return this.types[middle];
			}
			
			if (comparison > 0){
				start = middle + 1;
			}
			
			if (comparison < 0){
				end = middle - 1;
			}
		
			middle = (end + start) / 2;	
		}
		return null;
	}
		
	public static void createInstance(){
		if (instance != null) return;
		
		instance = new DBType();
	}
	
	public static DBType getInstance(){
		return instance;
	}
}
